<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="类型名称" prop="typeName">
        <el-input v-model="queryParams.typeName" placeholder="请输入类型名称" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>

      <el-form-item label="是否可见" prop="visible">
        <el-select v-model="queryParams.visible" placeholder="请选择是否可见" clearable class="!w-240px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.IS_NO)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <el-button type="primary" plain @click="openForm(undefined)" v-hasPermi="['wine:supply-type:create']">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button plain type="danger" @click="toggleExpandAll">
          <Icon class="mr-5px" icon="ep:sort" />
          展开/折叠
        </el-button>
        <!-- <el-button type="success" plain @click="handleExport" :loading="exportLoading"
          v-hasPermi="['wine:supply-type:export']">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button> -->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>

    <el-table v-if="refreshTable" v-loading="loading" :data="list" :default-expand-all="isExpandAll"
      :show-overflow-tooltip="true" row-key="id">
      <el-table-column label="类型名称" align="left" prop="typeName"/>
      <!-- <el-table-column label="主键编号" align="center" prop="id" /> -->
      <el-table-column label="是否可见" align="center" prop="visible"  width="100px">
        <template #default="{ row }">
          <el-switch :value="row.visible" :active-value="1" :inactive-value="0" active-text="是" inactive-text="否"
            inline-prompt @click="handleStatusChange(row)" />
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort"  width="100px"/>

      <!-- <el-table-column label="度数" align="center" prop="degrees">
        <template #default="scope">
          <el-tag type="primary" v-if="scope.row.degrees">{{ scope.row.degrees }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="年份" align="center" prop="showYears">
        <template #default="scope">
          <el-tag type="primary" v-if="scope.row.showYears">{{ scope.row.showYears }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="香型" align="center" prop="fragranceTypes">
        <template #default="scope">
          <el-tag type="primary" v-if="scope.row.fragranceTypes">{{ scope.row.fragranceTypes }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="容量" align="center" prop="capacities">
        <template #default="scope">
          <el-tag type="primary" v-if="scope.row.capacities">{{ scope.row.capacities }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="单位" align="center" prop="units">
        <template #default="scope">
          <el-tag type="primary" v-if="scope.row.units">{{ scope.row.units }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="标签" align="center" prop="labels">
        <template #default="scope">
          <el-tag type="primary" v-if="scope.row.labels">{{ scope.row.labels }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="包装" align="center" prop="packings">
        <template #default="scope">
          <el-tag type="primary" v-if="scope.row.packings">{{ scope.row.packings }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="产地" align="center" prop="producingAreas">
        <template #default="scope">
          <el-tag type="primary" v-if="scope.row.producingAreas">{{ scope.row.producingAreas }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="语言" align="center" prop="languages">
        <template #default="scope">
          <el-tag type="primary" v-if="scope.row.languages">{{ scope.row.languages }}</el-tag>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" width="180px">
        <template #default="scope">
          <el-button link type="primary" @click="openForm(scope.row.id)" v-hasPermi="['wine:supply-type:update']">
            编辑
          </el-button>
          <el-button v-hasPermi="['wine:supply-type:create']" link type="primary" @click="addForm(scope.row.id)">
            新增
          </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)" v-hasPermi="['wine:supply-type:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <!-- <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    /> -->
  </ContentWrap>

</template>

<script setup lang="ts">
import { SupplyTypeApi, SupplyTypeVO } from '@/api/wine/supplytype'
import { handleTree } from '@/utils/tree'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

/** 酒类类型 列表 */
defineOptions({ name: 'SupplyType' })
const { push } = useRouter() // 路由跳转
const isExpandAll = ref(false) // 是否展开，默认全部折叠

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<SupplyTypeVO[]>([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  parentId: undefined,
  typeName: undefined,
  depth: undefined,
  status: undefined,
  visible: undefined,
  sort: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SupplyTypeApi.getSupplyTypePage(queryParams)
    list.value = handleTree(data)

  } finally {
    loading.value = false
  }
}



const refreshTable = ref(true) // 重新渲染表格状态

/** 展开/折叠操作 */
const toggleExpandAll = () => {
  refreshTable.value = false
  isExpandAll.value = !isExpandAll.value
  nextTick(() => {
    refreshTable.value = true
  })
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const openForm = (id?: number) => {
  // 修改
  if (typeof id === 'number') {
    push({ name: 'SupplytypeEdit', params: { id } })
    return
  }
  // 新增
  push({ name: 'SupplytypeAdd' })
}

const addForm = (id?: number) => {
  push({ name: 'SupplytypeErAdd', params: { id } })

}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SupplyTypeApi.deleteSupplyType(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}



const handleStatusChange = async (row: any) => {
  try {
    let data = row
    // 二次确认
    const text = data.visible == 1 ? '不可见' : '可见'
    await message.confirm(`确认要${text}"${row.typeName}"吗？`).then(async (res) => {
      console.log("err", res);
      data.visible = data.visible == 1 ? 0 : 1
      // 发起修改
      await SupplyTypeApi.updateSupplyType(data)
      message.success('操作成功')
      // 刷新列表
      await getList()
    }).catch((err) => {
      console.log("err", err);
    })
  } catch {
    // 异常时，需要重置回之前的值
    await getList()
  }
}

// /** 导出按钮操作 */
// const handleExport = async () => {
//   try {
//     // 导出的二次确认
//     await message.exportConfirm()
//     // 发起导出
//     exportLoading.value = true
//     const data = await SupplyTypeApi.exportSupplyType(queryParams)
//     download.excel(data, '酒类类型.xls')
//   } catch {
//   } finally {
//     exportLoading.value = false
//   }
// }

onActivated(async () => {
  await getList()
})

/** 初始化 **/
onMounted(async () => {
  await getList()
})
</script>
